package com.chan.web.controller.cd;

import java.util.List;

import com.chan.common.annotation.Log;
import com.chan.common.core.controller.BaseController;
import com.chan.common.core.domain.AjaxResult;
import com.chan.common.core.domain.Ztree;
import com.chan.common.core.domain.entity.SysDept;
import com.chan.common.core.page.TableDataInfo;
import com.chan.common.enums.BusinessType;
import com.chan.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chan.domain.CdDocument;
import com.chan.service.ICdDocumentService;


/**
 * 文档库Controller
 *
 * @author chan
 * @date 2024-04-25
 */
@Controller
@RequestMapping("/cd/document")
public class CdDocumentController extends BaseController
{
    private String prefix = "cd/document";

    @Autowired
    private ICdDocumentService cdDocumentService;

    @RequiresPermissions("cd:document:view")
    @GetMapping()
    public String document()
    {
        return prefix + "/document";
    }


    @GetMapping("/itemTreeData")
    @ResponseBody
    public List<Ztree> itemTreeData()
    {
        List<Ztree> ztrees = cdDocumentService.selectItemTree();
        return ztrees;
    }
    /**
     * 查询文档库列表
     */
    @RequiresPermissions("cd:document:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(CdDocument cdDocument)
    {
        List<CdDocument> list = cdDocumentService.selectCdDocumentList(cdDocument);
        return AjaxResult.success(list);
    }

    /**
     * 导出文档库列表
     */
    @RequiresPermissions("cd:document:export")
    @Log(title = "文档库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CdDocument cdDocument)
    {
        List<CdDocument> list = cdDocumentService.selectCdDocumentList(cdDocument);
        ExcelUtil<CdDocument> util = new ExcelUtil<CdDocument>(CdDocument.class);
        return util.exportExcel(list, "文档库数据");
    }

    /**
     * 新增文档库
     */
    @GetMapping("/add/{relateId}")
    public String add(@PathVariable("relateId") String relateId,ModelMap mmap){
        System.out.println(relateId);
        mmap.put("relateId", relateId);
        return prefix + "/add";
    }

    /**
     * 新增保存文档库
     */
    @RequiresPermissions("cd:document:add")
    @Log(title = "文档库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CdDocument cdDocument)
    {
        return toAjax(cdDocumentService.insertCdDocument(cdDocument));
    }

    /**
     * 修改文档库
     */

    @GetMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, ModelMap mmap)
    {
        CdDocument cdDocument = cdDocumentService.selectCdDocumentById(id);
        mmap.put("cdDocument", cdDocument);
        return prefix + "/upload";
    }
    /**
     * 修改文档库
     */
    @RequiresPermissions("cd:document:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CdDocument cdDocument = cdDocumentService.selectCdDocumentById(id);
        mmap.put("cdDocument", cdDocument);
        return prefix + "/edit";
    }

    /**
     * 修改保存文档库
     */
    @RequiresPermissions("cd:document:edit")
    @Log(title = "文档库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CdDocument cdDocument)
    {
        return toAjax(cdDocumentService.updateCdDocument(cdDocument));
    }

    /**
     * 删除文档库
     */
    @RequiresPermissions("cd:document:remove")
    @Log(title = "文档库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cdDocumentService.deleteCdDocumentByIds(ids));
    }
}
